package com.cneiu.ppms.web.service;

import java.util.Map;

import net.sf.json.JSONObject;

import com.cneiu.ppms.web.base.BaseService;
import com.cneiu.ppms.web.model.LightBillDetailModel;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
/**
 * 电费详细
 * @author zhaokf
 * @date 2013-04-23
 *
 */
public class LightBillDetailService extends BaseService
{
	/**
	 * 分页列出电费详细
	 * @param page
	 * @param limit
	 * @param search
	 * @return
	 */
	public static Page<LightBillDetailModel> list(Map<String,String> params){
		JSONObject search = JSONObject.fromObject(params.get("search"));
		int limit = 0;
		int page = 1;
		try{
			limit = params.get("limit")==null?0:Integer.parseInt(params.get("limit").toString());
			page = params.get("page")==null?1:Integer.parseInt(params.get("page").toString());
		}catch (Exception e){
			limit = 100;
			page = 1;
		}
		String where = "";
		try{
			if (search.get("pd_prc_jfq") != null){
				where = " AND pd_prc_jfq=" + search.get("pd_prc_jfq");
			}
			if (search.get("pd_prc_code") != null){
				where = " AND pd_prc_code LIKE '%" + search.get("pd_prc_code") + "%'";
			}
			where = " WHERE " + where.substring(5);
		}catch (Exception e){
		}
		Page<LightBillDetailModel> bd = null;

		String select_sql = "SELECT * ";
		String query_sql = "FROM SUP_PRC_DETAIL " + where;

		try{
			bd = LightBillDetailModel.dao.paginate(page, limit, select_sql, query_sql);
		}catch (Exception e){
			setError(e.getMessage());
		}
		return bd;
	}



	/**
	 * 增加电费详细信息
	 * @param no
	 * @param name
	 * @param type
	 * @return
	 */
	public static boolean isAdd(Map<String,String> params){
		String SQL = "insert into SUP_PRC_DETAIL(pd_prc_jfq,pd_prc_code,pd_period,pd_timeIntv,pd_kwh) values(?,?,?,?,?)";
		int isupadded = Db.update(SQL, params.get("pd_prc_jfq"),params.get("pd_prc_code"),params.get("pd_period"),params.get("pd_timeIntv"),params.get("pd_kwh"));
		if(isupadded==1){
			return true;
		}
		return setError("你访问的电费详细信息已存在！");
	}

	/**
	 * 修改电费详细信息
	 * @param no
	 * @param name
	 * @param type
	 * @return
	 */
	public static boolean isUpdate(Map<String,String> params){
		String SQL = "update sup_prc_detail set pd_prc_jfq=?,pd_prc_code=?,pd_period=?,pd_timeIntv=?,pd_kwh=?";
		int isupadded = Db.update(SQL, params.get("pd_prc_jfq"),params.get("pd_prc_code"),params.get("pd_period"),params.get("pd_timeIntv"),params.get("pd_kwh"));
		if(isupadded==1){
			return true;
		}
		return setError("您访问的电费详细信息不存在！");
	}

	/**
	 * 删除电费详细信息
	 * @param no
	 * @return
	 */
	public static boolean isDelete(Map<String,String> params){
		String no = params.get("pd_prc_code")==null?"":params.get("pd_prc_code").toString();
		String sql = "select * from sup_prc_detail where pd_prc_code=?";
		LightBillDetailModel bd = LightBillDetailModel.dao.findFirst(sql, no);
		if (bd != null)
		{
			bd.delete();
			return true;
		}
		return setError("您访问的电费详细不存在！");
	}



	public static LightBillDetailModel get(Map<String, String> params)
	{
		String no = params.get("pd_prc_code")==null?"":params.get("pd_prc_code").toString();
		LightBillDetailModel ldm = LightBillDetailModel.dao.findFirst("SELECT * FROM SUP_PRC_DETAIL WHERE pd_prc_code=?", no);
		return ldm;
	}
}

